"""
fastapi 服务
"""
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from core.config import settings
from pathlib import Path
from loguru import logger

logger.add(Path(__file__).parent / "logs" / "info.log",  level='INFO',  rotation="50 MB",   retention="10 days")
logger.add(Path(__file__).parent / "logs" / "debug.log", level='DEBUG', rotation="500 MB",  retention="10 days")
logger.add(Path(__file__).parent / "logs" / "error.log", level='ERROR', rotation="50 MB",   retention="10 days")


def create_app():
    app = FastAPI(title="AIChat API Server", version=settings.VERSION)
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    mount_chat_routes(app)
    return app


def mount_chat_routes(app: FastAPI):

    from server.llm_answer import llm_answer_api
    from server.contract_parse import contract_parse_api
    from server.recall import recall_api
    from server.rule import rule_api
    from server.control import (
        correct_error_api,
        query_similar_text_api,
        train_model_api,
        predict_api,
        select_api
    )

    app.post(
        path='/chat/llmAnswer', summary="大模型问答"
    )(llm_answer_api)

    app.post(
        path='/chat/contractParse', summary="合同分拆入库"
    )(contract_parse_api)

    app.post(
        path='/chat/recall', summary="召回测试"
    )(recall_api)

    app.post(
        path='/chat/rule', summary="合同要素的内容抽取和语义审查接口"
    )(rule_api)

    app.post(
        path='/model/correctError', summary="错字审查"
    )(correct_error_api)

    app.post(
        path='/chat/embedSim', summary="相似文档检索"
    )(query_similar_text_api)

    app.post(
        path='/model/train', summary="模型训练"
    )(train_model_api)

    app.post(
        path='/model/predict', summary="模型预测"
    )(predict_api)

    app.post(
        path='/model/select', summary="模型选择"
    )(select_api)


my_app = create_app()


if __name__ == '__main__':
    uvicorn.run(app=my_app, host="0.0.0.0", port=5000, reload=False)
